package Parser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.io.*;

public class Stats implements java.lang.Comparable<Stats>{
	
	private String name;
	private int count;
	private float percent;
	
	private String workingDirectory;
	private String type;
	private String variante;
	private List<String> lines;
	private List<List<String>> data;
	
	public Stats() {}
	
	public Stats(String name, int count, float percent) {
		this.name = name;
		this.count = count;
		this.percent = percent;
	}
	
	public float getCount()
	{
		return count;
	}
	
	/**
	 * Create a stat file with first lines
	 * @throws IOException
	 */
	public void writeFile() throws IOException
	{		
		
		BufferedWriter stream = new BufferedWriter(new FileWriter(workingDirectory + "stat_" + variante + "_" + type + ".csv" , true));
		ArrayList<List<Stats>> allStat = new ArrayList<List<Stats>>();

		String title = "-18;-17;-16;-15;-14;-13;-12;-11;-10;-9;-8;-7;-6;-5;-4;-3;-2;-1;";
		for(int i=0; i<97; i++)
		{
			allStat.add(columnStat(i));
			if(i<79)
				title+= Integer.toString(i) + ";";
		}
		stream.write(title + "\n");
		String nameT = new String();
		String countT = new String();
		for(int i=0; i<97; i++)
		{
			for(int j=0; j<allStat.size(); j++)
			{
				List<Stats> stat = allStat.get(j);
				if(!stat.isEmpty() && stat.size() > i)
				{
					nameT += stat.get(i).name + ";";
					countT += stat.get(i).count + " / " + stat.get(i).percent + "%;";
				}
				else
				{
					nameT += " " + ";";
					countT += " " + ";";
				}
			}
			stream.write( nameT + "\n");
			stream.write( countT + "\n");
			nameT = "";
			countT = "";
		}
		stream.close();
	}
	
	/**
	 * create Stat for a column
	 * @param line
	 * @return
	 */
	public List<Stats> columnStat(int line)
	{
		Map <String, Integer> columnValues = new HashMap<String, Integer >();
		int nbTotal = 0;
		
		for(int i=0; i<data.size(); i++)
		{
			List<String> content = data.get(i);

			//System.out.println(content.size());
			if(content.size()>line)
			{
				String nameT = content.get(line);
				if(!nameT.isEmpty())
				{
					if(columnValues.containsKey(nameT))
						columnValues.put(nameT, columnValues.get(nameT) + 1);
					else
						columnValues.put(nameT, 1);
					nbTotal++;
				}
			}

		}
		List<Stats> list = new ArrayList<Stats>();
		
		for(Iterator<String> i = columnValues.keySet().iterator(); i.hasNext();)
		{
			String key = i.next();
			int value = Math.round((((float)columnValues.get(key)*100)/nbTotal)*100);
			list.add(new Stats(key,columnValues.get(key),(float)value/100));
		}
		
		Collections.sort(list, Collections.reverseOrder());
		
		/*for(int i=0; i<list.size(); i++)
		{
			Stats s = list.get(i);
			System.out.println(s.name + " " + s.count);
		}*/
		return list;
	}
	
	@Override 
	/**
	 * override function compare to
	 */
	public int compareTo(Stats stat) {
		float nb1 = ((Stats)stat).getCount();
		float nb2 = this.getCount();
		if(nb1>nb2) return -1;
		else if (nb1 == nb2) return 0;
		else return 1;	
	}
	
	
	/**
	 * Retrieve all trinucleotides from a file
	 * @param workingDir the working directory in which to look for file
	 * @param fileName the file name
	 * @param var the variant
	 */
	public void getAllTrinucleotides(String workingDir, String fileName, int var)
	{
		lines = readFile(workingDir + fileName + ".csv");
		type = fileName;
		workingDirectory = workingDir;
		variante = Integer.toString(var);
		data = new ArrayList<List<String>>();
		String separator = new Character(';').toString();
		for(String line : lines)
		{
			String[] oneData = line.split(separator);
			List<String> triNucleotides = new ArrayList<String>();

			if(var == 0)
			{
				for(int i=10; i<97; i++)
					triNucleotides.add(oneData[i]);
			}
			else if(var == 1 || var == 2)
			{
				for(int i=(10 + (var * 97)); i<(97 + (var * 97)); i++)
					triNucleotides.add(oneData[i]);
			}
			else 
			{
				for(int i=(10 + (var * 97)); i<oneData.length; i++)
					triNucleotides.add(oneData[i]);
			}
				
			data.add(triNucleotides);
		}
	}
	
	/**
	 * Read a file and retrieve all raw datas
	 * @param fileName
	 * @return
	 */
	public ArrayList<String> readFile(String fileName)
	{
		ArrayList<String> result = new ArrayList<String>();
		boolean titleLine = true;
		FileReader fr;
		try {
			fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);
			
			for(String line = br.readLine(); line != null; line = br.readLine())
			{
				if(titleLine)
					titleLine = false;
				else 
					result.add(line);
			}
			br.close();
			fr.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}
}
